import { LGXBDirection } from "../dir/LGXBDirection";
import { LGXBConfig } from "../../../resource/LGXBConfig";
import { AStarPoint } from "../../../scene/map/AStarPoint";
type int = number;
//class LGXBMap
    
    export  class LGXBMap
    {
        constructor()
        {
            
            this.createMapData();
            this.maxXLen = this._mapdata.length;
            this.maxYLen = this._mapdata[0].length;
            return;
        }

        public isThreeRoads(arg1: int, arg2: int): Boolean
        {
            var loc1=this.getThreeRoadPoints(LGXBDirection.toNone, arg1, arg2);
            return !(loc1 == null);
        }

        public getThreeRoads(): Array<any>
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc1=LGXBConfig.instance.map;
            var loc2=[];
            var loc6=0;
            var loc7=loc1;
            for(loc3 of loc7) 
            {
                if ((loc4 = this.getThreeRoadPoints(LGXBDirection.toNone, loc3.x, loc3.y)) == null) 
                {
                    continue;
                }
                loc5 = this.changeTileToPixelPoint(new AStarPoint(loc3.x, loc3.y));
                loc2.push(loc5);
            }
            return loc2;
        }

        /* internal  */createMapData(): void
        {
            var loc4=null;
            var loc5=0;
            var loc6=0;
            var loc7=0;
            var loc8=null;
            var loc9=0;
            this._mapdata = new Array<any>();
            var loc1=LGXBConfig.instance.map;
            var loc2=0;
            var loc3=0;
            var loc10=0;
            var loc11=loc1;
            for(loc4 of loc11) 
            {
                loc6 = loc4.x;
                loc7 = loc4.y;
                if (this._mapdata[loc6] == null) 
                {
                    this._mapdata[loc6] = new Array<any>();
                }
                this._mapdata[loc6][loc7] = 5;
                if (loc2 < loc6) 
                {
                    loc2 = loc6;
                }
                if (!(loc3 < loc7)) 
                {
                    continue;
                }
                loc3 = loc7;
            }
            ++loc2;
            ++loc3;
            loc5 = 0;
            while (loc5 < loc2) 
            {
                if ((loc8 = this._mapdata[loc5]) == null) 
                {
                    this._mapdata[loc5] = loc10 = new Array<any>();
                    loc8 = loc10;
                }
                loc9 = 0;
                while (loc9 < loc3) 
                {
                    if (loc8[loc9] != 5) 
                    {
                        loc8[loc9] = 0;
                    }
                    ++loc9;
                }
                ++loc5;
            }
            return;
        }

        public pointingNextRoadPoint(arg1: int, arg2: int, arg3: int): AStarPoint
        {
            var loc1=LGXBDirection.instance.getDirectionPoint(arg1);
            return new AStarPoint(arg2 + loc1.x, arg3 + loc1.y);
        }

        public getThreeRoadPoints(arg1: int, arg2: int, arg3: int): Array<any>
        {
            var loc5=undefined;
            var loc6=0;
            var loc1=LGXBDirection.instance.getOpersiteSide(arg1);
            var loc2=this.getNextFourRoad(arg2, arg3);
            var loc3=new Array<any>();
            if (loc1 != -1) 
            {
                loc3.push(loc1);
            }
            var loc4=0;
            var loc7=0;
            var loc8=loc2;
            for (loc5 in loc8) 
            {
                if (!((loc6 = loc2[loc5]) > 0)) 
                {
                    continue;
                }
                ++loc4;
                if (loc5 == loc1) 
                {
                    continue;
                }
                loc3.push(loc5);
            }
            if (loc3.length >= 3) 
            {
                return loc3;
            }
            return null;
        }

        public getNextFourRoad(arg1: int, arg2: int): Map<any, any> /* flash.utils.Dictionary */
        {
            var loc1=-1;
            var loc2=-1;
            var loc3=-1;
            var loc4=-1;
            if (arg2 > 0 && arg2 < (this.maxYLen - 1)) 
            {
                loc1 = this._mapdata[arg1][(arg2 - 1)];
                loc2 = this._mapdata[arg1][arg2 + 1];
            }
            else if (arg2 != 0) 
            {
                loc1 = this._mapdata[arg1][this.maxYLen - 2];
            }
            else 
            {
                loc2 = this._mapdata[arg1][1];
            }
            if (arg1 > 0 && arg1 < (this.maxXLen - 1)) 
            {
                loc3 = this._mapdata[(arg1 - 1)][arg2];
                loc4 = this._mapdata[arg1 + 1][arg2];
            }
            else if (arg1 != 0) 
            {
                loc3 = this._mapdata[this.maxXLen - 2][arg2];
            }
            else 
            {
                loc4 = this._mapdata[1][arg2];
            }
            var loc5;
            (loc5 = new Map<any, any> /* flash.utils.Dictionary */(false))[LGXBDirection.toLeft] = loc1;
            loc5[LGXBDirection.toRight] = loc2;
            loc5[LGXBDirection.toTop] = loc3;
            loc5[LGXBDirection.toDown] = loc4;
            return loc5;
        }

        public autoFindNextRoadPointDirect(arg1: int, arg2: int, arg3: int): int
        {
            var loc4=undefined;
            var loc5=0;
            var loc6=0;
            var loc1=this.getNextFourRoad(arg2, arg3);
            var loc2=1;
            var loc3=[];
            var loc7=0;
            var loc8=loc1;
            for (loc4 in loc8) 
            {
                if (!((loc5 = loc1[loc4]) > 0)) 
                {
                    continue;
                }
                loc3.push(loc4);
            }
            if (loc3.length == 2) 
            {
                loc6 = LGXBDirection.instance.getOpersiteSide(arg1);
                if (loc3[0] == loc6) 
                {
                    return loc3[1];
                }
                if (loc3[1] == loc6) 
                {
                    return loc3[0];
                }
                if (loc3[0] == arg1) 
                {
                    return loc3[0];
                }
                if (loc3[1] == arg1) 
                {
                    return loc3[1];
                }
                throw new Error("找不到方向<from LGXBMap.as>");
            }
            return LGXBDirection.toNone;
        }

        public changePixelToTilePoint(arg1: Object /* flash.geom.Point */): AStarPoint
        {
            var loc1=new AStarPoint();
            loc1.y = int((arg1.x - this.offx) / (this.grid_w + this.gridblank_w));
            loc1.x = int((arg1.y - this.offy) / (this.grid_h + this.gridblank_h));
            if (loc1.y < 0) 
            {
                loc1.x = 0;
            }
            if (loc1.y > (this.maxYLen - 1)) 
            {
                loc1.y = (this.maxYLen - 1);
            }
            if (loc1.x < 0) 
            {
                loc1.x = 0;
            }
            if (loc1.x > (this.maxXLen - 1)) 
            {
                loc1.x = (this.maxXLen - 1);
            }
            return loc1;
        }

        public changeTileToPixelPoint(arg1: Object): Object /* flash.geom.Point */
        {
            var loc1=new Object /* flash.geom.Point */();
            loc1.x = this.offx + arg1.y * (this.grid_w + this.gridblank_w) + this.grid_w / 2;
            loc1.y = this.offy + arg1.x * (this.grid_h + this.gridblank_h) + this.grid_h / 2;
            return loc1;
        }

        public /* const */offx: int=27;

        public /* const */offy: int=23;

        public /* const */grid_w: int=66;

        public /* const */grid_h: int=44;

        public /* const */gridblank_w: int=6;

        public /* const */gridblank_h: int=7;

        public static /* const */instance: LGXBMap=new LGXBMap();

        /* internal */ /* var */_mapdata: Array<any>;

        /* internal */ /* var */maxXLen: int;

        /* internal */ /* var */maxYLen: int;
    }
